VERSION 0.6

ARG DOCKERHUB_USER_SECRET=+secrets/DOCKERHUB_USER
ARG DOCKERHUB_TOKEN_SECRET=+secrets/DOCKERHUB_TOKEN
ARG DOCKERHUB_MIRROR
ARG DOCKERHUB_MIRROR_INSECURE=false
ARG DOCKERHUB_AUTH=true

FROM ..+base \
    --DOCKERHUB_AUTH=$DOCKERHUB_AUTH \
    --DOCKERHUB_USER_SECRET=$DOCKERHUB_USER_SECRET \
    --DOCKERHUB_TOKEN_SECRET=$DOCKERHUB_TOKEN_SECRET \
    --DOCKERHUB_MIRROR=$DOCKERHUB_MIRROR \
    --DOCKERHUB_MIRROR_INSECURE=$DOCKERHUB_MIRROR_INSECURE

COPY test-secret-provider /weird/path/.
RUN echo "#!/bin/sh
set -ex
export PATH=/weird/path:\$PATH
earthly --config \$earthly_config config global.secret_provider test-secret-provider
earthly --config \$earthly_config --verbose -D +test
" >/tmp/test-earthly-script && chmod +x /tmp/test-earthly-script

test:
    DO +RUN_EARTHLY_ARGS --earthfile=test.earth --exec_cmd=/tmp/test-earthly-script
    RUN test "$(cat output/value)" = "my secret is williwaw; don't tell anyone."

test-binary:
    DO +RUN_EARTHLY_ARGS --earthfile=test-binary.earth --exec_cmd=/tmp/test-earthly-script

test-all:
    BUILD +test
    BUILD +test-binary

RUN_EARTHLY_ARGS:
    COMMAND
    ARG earthfile
    ARG pre_command
    ARG exec_cmd
    DO ..+RUN_EARTHLY \
        --earthfile=$earthfile \
        --pre_command=$pre_command \
        --exec_cmd=$exec_cmd \
        --DOCKERHUB_AUTH=$DOCKERHUB_AUTH \
        --DOCKERHUB_USER_SECRET=$DOCKERHUB_USER_SECRET \
        --DOCKERHUB_TOKEN_SECRET=$DOCKERHUB_TOKEN_SECRET \
        --DOCKERHUB_MIRROR=$DOCKERHUB_MIRROR \
        --DOCKERHUB_MIRROR_INSECURE=$DOCKERHUB_MIRROR_INSECURE
